import { searchArticleApi, searchSuggestApi } from '@/api/search';
import { AppDispatch } from '..';
import { searchActionType } from '../reducer/search';

//获取/保存搜索结果
export const searchAction = (params: {
  page?: number;
  per_page?: number;
  q: string;
}) => {
  return async (dispatch: AppDispatch) => {
    const res = await searchArticleApi(params);
    dispatch({ type: 'search/result', payload: res.data.results });
  };
};
//保存搜索历史
export const historySearchAction = (searchKey: string): searchActionType => {
  return { type: 'search/history', payload: searchKey };
};
//清除搜索历史
export const clearHistoryAction = (): searchActionType => {
  return { type: 'search/clear' };
};
//搜索建议
export const searchSuggestAction = (searchKey: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await searchSuggestApi({ q: searchKey.trim() });
    dispatch({ type: 'search/suggest', payload: res.data.options });
  };
};
//清除建议
export const clesrSuggestAction = (): searchActionType => {
  return { type: 'search/clearSuggest' };
};
//清除搜索结果
export const clearResultAction = (): searchActionType => {
  return { type: 'search/clearRessult' };
};
